package myschool.service;

import myschool.dao.SubjectDao;
import myschool.entity.Page;
import myschool.entity.Subject;
import myschool.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class SubjectService {
    public static final SubjectService ME = new SubjectService();

    public static final Logger Log = LoggerFactory.getLogger(SubjectService.class);

    public int add(Subject s) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            return dao.add(s);
        } catch (Exception e) {
            Log.error("增加失败", e);
            return 0;//表示失败
        } finally {
            MybatisUtil.close(session);
        }
    }

    public int update(Subject s) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            return dao.update(s);
        } catch (Exception e) {
            Log.error("增加失败", e);
            return 0;//表示失败
        } finally {
            MybatisUtil.close(session);
        }
    }

    //public int updateHours( long id, @Param("hours") int hours);

    public int del(Long id) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            return dao.del(id);
        } catch (Exception e) {
            Log.error("增加失败", e);
            return 0;//表示失败
        } finally {
            MybatisUtil.close(session);
        }
    }

    public Subject get(Long id) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            return dao.get(id);
        } finally {
            MybatisUtil.close(session);
        }
    }

    public Page<Subject> page(int pageNo, int pageSize, String sname) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            List<Subject> list = dao.paging(pageNo, pageSize, sname);
            int total = dao.findTotal(sname);
            return new Page<>(list, pageNo, total, pageSize);
        } finally {
            MybatisUtil.close(session);
        }
    }


    //
//    public int findTotal() {
//        SqlSession session = MybatisUtil.get();
//        try {
//            SubjectDao dao = session.getMapper(SubjectDao.class);
//            return dao.findTotal();
//        } finally {
//            MybatisUtil.close(session);
//        }
//    }
    //true表示重复了。。。false没有
    public boolean checkNameRepeat(String sname) {
        SqlSession session = MybatisUtil.get();
        try {
            SubjectDao dao = session.getMapper(SubjectDao.class);
            return dao.checkName(sname) > 0;
        } finally {
            MybatisUtil.close(session);
        }
    }
}
